﻿<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
  
  <head>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-39155502-5"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
  
      gtag('config', 'UA-39155502-5');
    </script>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Code Samples | MongoDB.Entities </title>
    <meta name="viewport" content="width=device-width">
    <meta name="title" content="Code Samples | MongoDB.Entities ">
    <meta name="generator" content="docfx 2.58.0.0">
    <meta name="description" content="A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management.">
    <link rel="shortcut icon" href="../images/favicon.ico">
    <link rel="stylesheet" href="../styles/docfx.vendor.css">
    <link rel="stylesheet" href="../styles/docfx.css">
    <link rel="stylesheet" href="../styles/main.css">
    <meta property="docfx:navrel" content="../toc.html">
    <meta property="docfx:tocrel" content="../toc.html">
    
    <meta property="docfx:rel" content="../">
    <meta property="docfx:newtab" content="true">
    <meta property="og:title" content="MongoDB.Entities">
    <meta property="og:site_name" content="MongoDB.Entities">
    <meta property="og:url" content="https://mongodb-entities.com">
    <meta property="og:description" content="A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management,">
    <meta property="og:type" content="website">
    <meta property="og:image" content="https://mongodb-entities.com/images/social-square.png">  
  </head>
  <body data-spy="scroll" data-target="#affix" data-offset="120">
    <div id="wrapper">
      <header>
        
        <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              
              <a class="navbar-brand" href="../index.html">
                <img id="logo" class="svg" src="../images/icon.png" alt="">
              </a>
            </div>
            <div class="collapse navbar-collapse" id="navbar">
              <form class="navbar-form navbar-right" role="search" id="search">
                <div class="form-group">
                  <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
                </div>
              </form>
            </div>
          </div>
        </nav>
        
        <div class="subnav navbar navbar-default">
          <div class="container hide-when-search" id="breadcrumb">
            <ul class="breadcrumb">
              <li></li>
            </ul>
          </div>
        </div>
      </header>
      <div class="container body-content">
        
        <div id="search-results">
          <div class="search-list">Search Results for <span></span></div>
          <div class="sr-items">
            <p><i class="glyphicon glyphicon-refresh index-loading"></i></p>
          </div>
          <ul id="pagination" data-first="First" data-prev="Previous" data-next="Next" data-last="Last"></ul>
        </div>
      </div>
      <div role="main" class="container body-content hide-when-search">
        <div class="article row grid">
          <div class="col-md-10">
            <article class="content wrap" id="_content" data-uid="">
<h1 id="code-samples">Code Samples</h1>

<hr>
<h3 id="initialize-connection">Initialize connection</h3>
<pre><code class="lang-csharp">  await DB.InitAsync(&quot;bookshop&quot;,&quot;localhost&quot;);
</code></pre>
<h3 id="persist-an-entity">Persist an entity</h3>
<pre><code class="lang-csharp">  var book = new Book { Title = &quot;The Power Of Now&quot; };
  await book.SaveAsync();
</code></pre>
<h3 id="embed-as-document">Embed as document</h3>
<pre><code class="lang-csharp">  var dickens = new Author { Name = &quot;Charles Dickens&quot; };
  book.Author = dickens.ToDocument();
  await book.SaveAsync();
</code></pre>
<h3 id="update-entity-properties">Update entity properties</h3>
<pre><code class="lang-csharp">  await DB.Update&lt;Book&gt;()
          .Match(b =&gt; b.Title == &quot;The Power Of Now&quot;)
          .Modify(b =&gt; b.Publisher, &quot;New World Order&quot;)
          .Modify(b =&gt; b.ISBN, &quot;SOMEISBNNUMBER&quot;)
          .ExecuteAsync();
</code></pre>
<h3 id="one-to-one-relationship">One-To-One relationship</h3>
<pre><code class="lang-csharp">  var hemmingway = new Author { Name = &quot;Ernest Hemmingway&quot; };
  await hemmingway.SaveAsync();
  book.MainAuthor = hemmingway;
  await book.SaveAsync();
</code></pre>
<h3 id="one-to-many-relationship">One-To-Many relationship</h3>
<pre><code class="lang-csharp">  var tolle = new Author { Name = &quot;Eckhart Tolle&quot; };
  await tolle.SaveAsync();
  await book.Authors.AddAsync(tolle);
</code></pre>
<h3 id="many-to-many-relationship">Many-To-Many relationship</h3>
<pre><code class="lang-csharp">  var genre = new Genre { Name = &quot;Self Help&quot; };
  await genre.SaveAsync();
  await book.AllGenres.AddAsync(genre);
  await genre.AllBooks.AddAsync(book);
</code></pre>
<h3 id="queries">Queries</h3>
<pre><code class="lang-csharp">  var author = await DB.Find&lt;Author&gt;().OneAsync(&quot;ID&quot;);

  var authors = await DB.Find&lt;Author&gt;().ManyAsync(a =&gt; a.Publisher == &quot;Harper Collins&quot;);

  var eckhart = await DB.Queryable&lt;Author&gt;()
                        .Where(a =&gt; a.Name.Contains(&quot;Eckhart&quot;))
                        .SingleOrDefaultAsync();

  var powerofnow = await genre.AllBooks
                              .ChildrenQueryable()
                              .Where(b =&gt; b.Title.Contains(&quot;Power&quot;))
                              .SingleOrDefaultAsync();

  var selfhelp = await book.AllGenres
                           .ChildrenQueryable()
                           .FirstAsync();
</code></pre>
<h3 id="delete">Delete</h3>
<pre><code class="lang-csharp">  await book.MainAuthor.DeleteAsync();
  await book.AllAuthors.DeleteAllAsync();
  await book.DeleteAsync();
  await DB.DeleteAsync&lt;Genre&gt;(&quot;ID&quot;);
  await DB.DeleteAsync&lt;Book&gt;(b =&gt; b.Title == &quot;The Power Of Now&quot;);
</code></pre>
<hr>
<div class="actions-container">
  <div><a href="Get-Started.html">Get Started</a></div>
</div>
<hr>
<h1 id="tutorials">Tutorials</h1>
<ul>
<li><a href="https://dev.to/djnitehawk/tutorial-mongodb-with-c-the-easy-way-1g68">Beginners Guide</a></li>
<li><a href="https://dev.to/djnitehawk/mongodb-fuzzy-text-search-with-c-the-easy-way-3l8j">Fuzzy Text Search</a></li>
<li><a href="https://dev.to/djnitehawk/tutorial-geospatial-search-in-mongodb-the-easy-way-kbd">GeoSpatial Search</a></li>
</ul>
<hr>
<h1 id="more-examples">More Examples</h1>
<ul>
<li><a href="https://github.com/dj-nitehawk/MongoWebApiStarter">Asp.net core web-api project</a></li>
<li><a href="https://github.com/dj-nitehawk/MongoDB-Entities-Repository-Pattern">Repository pattern project</a></li>
<li><a href="https://gist.github.com/dj-nitehawk">A collection of gists</a></li>
<li><a href="https://github.com/dj-nitehawk/MongoDB.Entities/tree/master/Tests">Integration/unit test project</a></li>
<li><a href="https://stackoverflow.com/search?tab=newest&amp;q=user%3a4368485%20%5bmongodb%5d">Solutions to stackoverflow questions</a></li>
</ul>
</article>
          </div>
          
          <div class="hidden-sm col-md-2" role="complementary">
            <div class="sideaffix">
              <div class="contribution">
                <ul class="nav">
                </ul>
              </div>
              <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
                <h5>In This Article</h5>
                <div></div>
              </nav>
            </div>
          </div>
        </div>
      </div>
      
      <footer>
        <div class="grad-bottom"></div>
        <div class="footer">
          <div class="container">
            <span class="pull-right">
              <a href="#top">Back to top</a>
            </span>
            Developed by <a href='https://github.com/dj-nitehawk'>Đĵ ΝιΓΞΗΛψΚ</a> and <a href='https://github.com/dj-nitehawk/MongoDB.Entities/graphs/contributors'>contributors</a> / Licensed under <a href='https://github.com/dj-nitehawk/MongoDB.Entities/blob/master/LICENSE'>MIT</a> / Website generated by <a href='https://dotnet.github.io/docfx/'>DocFX</a>
            
          </div>
        </div>
      </footer>
    </div>
    
    <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
    <script type="text/javascript" src="../styles/docfx.js"></script>
    <script type="text/javascript" src="../styles/main.js"></script>
  </body>
</html>
